package com.tjh.hive_data_compare;

import com.tjh.hive_data_compare.util.FileUtil;
import com.tjh.hive_data_compare.util.HiveUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by tjh.
 * Date: 2021/6/15
 **/
public class TableNumber {
    public static void main(String[] args) throws Exception {
     /*   String address = "192.168.83.176";
        String filePath = "D:\\tt.txt";
        String sampleDb = "dw_ods";*/

        String address = "10.80.17.152";
        String filePath = "/opt/qianyi/jar/read.txt";
        String sampleDb = "dw_ods";
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String currenDate = sdf.format(date);
        currenDate = currenDate.replaceAll("\\:", "").replaceAll(" ", "");
        if (args != null && args.length > 2) {
            address = args[0];
            filePath = args[1];
            sampleDb = args[2];
        } else {
            System.out.println("参数不对,退出");
        }
        List<String> tableNameList = FileUtil.readFileByLines(filePath);
        if (tableNameList != null && tableNameList.size() > 0) {
            System.out.println("获取表名:" + tableNameList.size() + "个");
            HiveUtil hiveUtil_2 = new HiveUtil();
            hiveUtil_2.init(address, "", "", sampleDb);
            for (String tmpTable : tableNameList) {
                try {
                    System.out.println("开始处理:" + tmpTable);
                    int resultCount = hiveUtil_2.count(tmpTable);
                    System.out.println("tmpTable:" + resultCount);
                    FileUtil.appendMethodB(currenDate + "_tablecount.txt", tmpTable + " " + resultCount + "\n");
                } catch (Exception ex) {
                    FileUtil.appendMethodB(currenDate + "_tablecount.txt", tmpTable + "异常:" + ex + "\n");
                }
            }

            hiveUtil_2.destory();
        } else {
            System.out.println("未找到表名");
        }
    }
}
